package org.elcom.source;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.TimerTask;

import oracle.jdbc.pool.OracleDataSource;

public class Connect extends TimerTask {

    public static String address = Config.address;
    public static int port = Config.port;
    public static String sid = Config.sid;
    public static String userName = Config.userName;
    public static String password = Config.password;
    public static Connection connect = null;
    private static boolean result = false;

    @Override
    public void run() {
        try {
            if (connect == null) {
                connect = connectToDB();
//                System.out.println("Connect success");
//                connect.close();
            } else {
                System.out.println(".");
            }

            ParseCDRFile.parseFile(Config.sourceDir);
//            ParseCDRFile.retryInsertFromError();
//            System.gc();
//            connect.close();
        } catch (Exception ex) {
            ex.printStackTrace();
//            reconnectToDB(Config.time);
            connectToDB();
        } finally {
//        	connect = null;
        }

    }

    public static Connection connectToDB() {
        Connection con = connect;

        try {
            OracleDataSource ods = new OracleDataSource();
            ods.setDriverType("thin");
            ods.setServerName(address);
            ods.setPortNumber(port);
            ods.setDatabaseName(sid);
            ods.setUser(userName);
            ods.setPassword(password);
//                ods.setURL("jdbc:oracle:thin:@" + address + ":" + port + ":" + sid);
//                con = ods.getConnection(userName, password);
            con = ods.getConnection();
//                System.out.println(con.toString());
            con.setAutoCommit(false);

            result = true;
//                connect.close();
        } catch (Exception e) {
            result = false;
//                System.out.println("Connect failed");
            reconnectToDB(Config.time);
//                  con.close();
        }
        if (!result) {
            try {
                Thread.sleep(2000);
                connectToDB();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
//        while (result == false){
//            reconnectToDB(Config.time);
//        }
        return con;

    }

    public static void reconnectToDB(int time) {
        int waited = 0;
        System.out.println("Reconnecting...");
        while (waited < time) {
            try {
                OracleDataSource ods = new OracleDataSource();
                ods.setDriverType("thin");
                ods.setServerName(address);
                ods.setPortNumber(port);
                ods.setDatabaseName(sid);
                ods.setUser(userName);
                ods.setPassword(password);
                connect = ods.getConnection();
                result = true;
            } catch (SQLException e) {
                result = false;
//                e.printStackTrace();
//                System.out.println("Error");
            }
            if (connect == null) {
                result = false;
                waited += 1000;
            }
        }
        if (!result) {
            System.out.println("Reconnect failed");
        } else {
            System.out.println("Reconnect success");
        }

    }
}
